using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Data;
using System.Text;
using System.IO;
using ExceptionHandler;
using System.Configuration;
using System.Globalization;
using System.Diagnostics;
using Microsoft.ApplicationBlocks.ExceptionManagement;
namespace ASUV
{
    public class ASQueryListForm : ASUV.ASChildForm
	{
		# region [members]
		private System.ComponentModel.IContainer components = null;
		private Label label1;
		private Button executeButton;
		private Button closeQueryListButton;
		private DataGridView queryListDataGridView;
		private GroupBox queryListGroupBox;
		ProjectFileFoundEventArgs projectDetails;
		Boolean showAS2K5ListStatus;
		private Button addQueryButton;
		private Button removeAllButton;
		private ContextMenuStrip removeContextMenuStrip;
		private ToolStripMenuItem removeToolStripMenuItem;
		private ComboBox asInstanceComboBox;
		bool customize,queryExist,as2kQueryStatus,as2k5QueryStatus;
		ASMainForm mainForm;
		bool connStatus;
		public delegate void QueriesRemovedEvent();
		public event QueriesRemovedEvent QueriesRemoved=null;
		#endregion

		#region public ASQueryListForm(ProjectFileFoundEventArgs args,ASMainForm asMainForm)
		public ASQueryListForm(ProjectFileFoundEventArgs args,ASMainForm asMainForm)
        {
            // This call is required by the Windows Form Designer.
            projectDetails = args;
            InitializeComponent();
			mainForm = asMainForm;

            // TODO: Add any initialization after the InitializeComponent call
		}
		#endregion

		#region protected override void Dispose(bool disposing)
		/// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
		}
		#endregion

		#region Designer generated code
		/// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
			this.components = new System.ComponentModel.Container();
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
			this.label1 = new System.Windows.Forms.Label();
			this.asInstanceComboBox = new System.Windows.Forms.ComboBox();
			this.executeButton = new System.Windows.Forms.Button();
			this.closeQueryListButton = new System.Windows.Forms.Button();
			this.queryListDataGridView = new System.Windows.Forms.DataGridView();
			this.removeContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
			this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
			this.queryListGroupBox = new System.Windows.Forms.GroupBox();
			this.addQueryButton = new System.Windows.Forms.Button();
			this.removeAllButton = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.queryListDataGridView)).BeginInit();
			this.removeContextMenuStrip.SuspendLayout();
			this.queryListGroupBox.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.label1.Location = new System.Drawing.Point(6, 30);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(0, 0);
			this.label1.TabIndex = 0;
			// 
			// asInstanceComboBox
			// 
			this.asInstanceComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.asInstanceComboBox.FormattingEnabled = true;
			this.asInstanceComboBox.Items.AddRange(new object[] {
            "Analysis Services 2000",
            "Analysis Services 2005"});
			this.asInstanceComboBox.Location = new System.Drawing.Point(201, 27);
			this.asInstanceComboBox.MaxDropDownItems = 2;
			this.asInstanceComboBox.Name = "asInstanceComboBox";
			this.asInstanceComboBox.Size = new System.Drawing.Size(143, 21);
			this.asInstanceComboBox.TabIndex = 1;
			this.asInstanceComboBox.SelectedIndexChanged += new System.EventHandler(this.asInstanceComboBox_SelectedIndexChanged);
			// 
			// executeButton
			// 
			this.executeButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.executeButton.Location = new System.Drawing.Point(126, 518);
			this.executeButton.Name = "executeButton";
			this.executeButton.Size = new System.Drawing.Size(105, 23);
			this.executeButton.TabIndex = 3;
			this.executeButton.Text = "Execute Queries";
			this.executeButton.Click += new System.EventHandler(this.executeButton_Click);
			// 
			// closeQueryListButton
			// 
			this.closeQueryListButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.closeQueryListButton.Location = new System.Drawing.Point(587, 518);
			this.closeQueryListButton.Name = "closeQueryListButton";
			this.closeQueryListButton.Size = new System.Drawing.Size(95, 23);
			this.closeQueryListButton.TabIndex = 5;
			this.closeQueryListButton.Text = "Close";
			this.closeQueryListButton.Click += new System.EventHandler(this.closeQueryListButton_Click);
			// 
			// queryListDataGridView
			// 
			this.queryListDataGridView.AllowUserToAddRows = false;
			this.queryListDataGridView.AllowUserToDeleteRows = false;
			this.queryListDataGridView.AllowUserToOrderColumns = true;
			this.queryListDataGridView.AllowUserToResizeRows = false;
			this.queryListDataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
			dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
			dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
			dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
			dataGridViewCellStyle1.FormatProvider = new System.Globalization.CultureInfo("en-US");
			dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
			dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
			dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
			this.queryListDataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
			this.queryListDataGridView.ColumnHeadersHeight = 18;
			this.queryListDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
			dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
			dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
			dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
			dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
			this.queryListDataGridView.DefaultCellStyle = dataGridViewCellStyle2;
			this.queryListDataGridView.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
			this.queryListDataGridView.Location = new System.Drawing.Point(7, 87);
			this.queryListDataGridView.MultiSelect = false;
			this.queryListDataGridView.Name = "queryListDataGridView";
			this.queryListDataGridView.ReadOnly = true;
			this.queryListDataGridView.RowHeadersVisible = false;
			this.queryListDataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
			this.queryListDataGridView.Size = new System.Drawing.Size(675, 395);
			this.queryListDataGridView.TabIndex = 6;
			// 
			// removeContextMenuStrip
			// 
			this.removeContextMenuStrip.Enabled = true;
			this.removeContextMenuStrip.GripMargin = new System.Windows.Forms.Padding(2);
			this.removeContextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.removeToolStripMenuItem});
			this.removeContextMenuStrip.Location = new System.Drawing.Point(25, 59);
			this.removeContextMenuStrip.Name = "removeContextMenuStrip";
			this.removeContextMenuStrip.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.removeContextMenuStrip.Size = new System.Drawing.Size(106, 26);
			// 
			// removeToolStripMenuItem
			// 
			this.removeToolStripMenuItem.Name = "removeToolStripMenuItem";
			this.removeToolStripMenuItem.Text = "Remove ";
			this.removeToolStripMenuItem.Click += new System.EventHandler(this.removeToolStripMenuItem_Click);
			// 
			// queryListGroupBox
			// 
			this.queryListGroupBox.Controls.Add(this.addQueryButton);
			this.queryListGroupBox.Controls.Add(this.removeAllButton);
			this.queryListGroupBox.Controls.Add(this.queryListDataGridView);
			this.queryListGroupBox.Controls.Add(this.closeQueryListButton);
			this.queryListGroupBox.Controls.Add(this.executeButton);
			this.queryListGroupBox.Controls.Add(this.asInstanceComboBox);
			this.queryListGroupBox.Controls.Add(this.label1);
			this.queryListGroupBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.queryListGroupBox.Location = new System.Drawing.Point(15, 12);
			this.queryListGroupBox.Name = "queryListGroupBox";
			this.queryListGroupBox.Size = new System.Drawing.Size(688, 550);
			this.queryListGroupBox.TabIndex = 2;
			this.queryListGroupBox.TabStop = false;
			this.queryListGroupBox.Text = "List of Queries";
			// 
			// addQueryButton
			// 
			this.addQueryButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.addQueryButton.Location = new System.Drawing.Point(6, 518);
			this.addQueryButton.Name = "addQueryButton";
			this.addQueryButton.Size = new System.Drawing.Size(105, 23);
			this.addQueryButton.TabIndex = 2;
			this.addQueryButton.Text = "Add Query";
			this.addQueryButton.Click += new System.EventHandler(this.addQueryButton_Click);
			// 
			// removeAllButton
			// 
			this.removeAllButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.removeAllButton.Location = new System.Drawing.Point(249, 518);
			this.removeAllButton.Name = "removeAllButton";
			this.removeAllButton.Size = new System.Drawing.Size(95, 23);
			this.removeAllButton.TabIndex = 4;
			this.removeAllButton.Text = "Remove All";
			this.removeAllButton.Click += new System.EventHandler(this.removeAllButton_Click);
			// 
			// ASQueryListForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.ClientSize = new System.Drawing.Size(709, 615);
			this.Controls.Add(this.queryListGroupBox);
			this.Location = new System.Drawing.Point(0, 0);
			this.Name = "ASQueryListForm";
			this.Text = "Query List";
			this.Shown += new System.EventHandler(this.ASQueryListForm_Shown);
			this.Load += new System.EventHandler(this.ASQueryListForm_Load);
			((System.ComponentModel.ISupportInitialize)(this.queryListDataGridView)).EndInit();
			this.removeContextMenuStrip.ResumeLayout(false);
			this.queryListGroupBox.ResumeLayout(false);
			this.queryListGroupBox.PerformLayout();
			this.ResumeLayout(false);

		}
        #endregion

		#region private void ASQueryListForm_Load(object sender, EventArgs e)
		private void ASQueryListForm_Load(object sender, EventArgs e)
        {
			XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);			

			if (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries").HasChildNodes)
			{
				customize = true;
				queryExist = true;
			}
			if (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[AS2KQueryStatus='Execution Succeeded']") != null) as2kQueryStatus = true;
			if (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[AS2K5QueryStatus='Execution Succeeded']") != null) as2k5QueryStatus=true;
			if (mainForm.show2005==true) asInstanceComboBox.SelectedIndex = 1;
			else asInstanceComboBox.SelectedIndex = 0;
		}
		#endregion

		#region private void ListQuery()

		/// <summary>
		/// Load all queries into DataGridView
		/// </summary>
        private void ListQuery()
        {
            DataSet ds = new DataSet();
			ds.Locale = CultureInfo.InvariantCulture;
			if (queryExist)
			{
				//StringReader stringReaderQueryList = new StringReader(xmlDomProjectFile.SelectSingleNode("ProjectFile/MDXQueries").OuterXml.Replace("\r\n", ""));
				//ds.ReadXml(stringReaderQueryList);
				customize = true;


				XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
				XmlReader xmlReadQueryList = new XmlNodeReader(xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries"));
				ds.ReadXml(xmlReadQueryList);
				xmlReadQueryList.Close();
				ds.Tables[0].Columns.Remove("ID");
				ds.Tables[0].Columns.Remove("CubeName");
				ds.Tables[0].Columns.Remove("NumberOfCells");
				ds.Tables[0].Columns.Remove("AS2KResult");
				ds.Tables[0].Columns.Remove("AS2K5Result");
				ds.Tables[0].Columns.Remove("ComparisonResult");
				if (showAS2K5ListStatus == true)
				{
					ds.Tables[0].Columns.Remove("QueryAS2K");
					ds.Tables[0].Columns.Remove("AS2KQueryStatus");
					ds.Tables[0].Columns.Remove("AS2KExecDuration");
				}
				else
				{
					ds.Tables[0].Columns.Remove("QueryAS2K5");
					ds.Tables[0].Columns.Remove("AS2K5QueryStatus");
					ds.Tables[0].Columns.Remove("AS2K5ExecDuration");
				}
				queryListDataGridView.DataSource = ds.Tables[0];
				int count = queryListDataGridView.RowCount;
				for (int counter = 0; counter < count; counter++)
				{
					string query = queryListDataGridView.Rows[counter].Cells[2].Value.ToString().Trim().Replace("amp;", "&");
					query = query.Replace("lt;", "<");
					query = query.Replace("gt;", ">");
					query = query.Replace("quote;", "\"");
					queryListDataGridView.Rows[counter].Cells[2].Value = query;
					query = queryListDataGridView.Rows[counter].Cells[1].Value.ToString().Trim().Replace("amp;", "&");
					query = query.Replace("lt;", "<");
					query = query.Replace("gt;", ">");
					query = query.Replace("quote;", "\"");
					queryListDataGridView.Rows[counter].Cells[1].Value = query;
				}
				executeButton.Enabled = true;
				removeAllButton.Enabled = true;
			}
			else
			{
				queryListDataGridView.Columns.Clear();
				queryListDataGridView.Columns.Add("Message", "Please add queries to the project");
				executeButton.Enabled = false;
				removeAllButton.Enabled = false;
			}

		}
		#endregion

		#region private void asInstanceComboBox_SelectedIndexChanged(object sender, EventArgs e)
		/// <summary>
		/// List queries according to the selected instance in the the combobox 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void asInstanceComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (asInstanceComboBox.Text.Trim() == "Analysis Services 2005")
			{
				showAS2K5ListStatus = true;
				ListQuery();
				CustomizeGrid();
			}
			else
			{
				showAS2K5ListStatus = false;
				ListQuery();
				CustomizeGrid();
			}
		}
		#endregion

		#region private void executeButton_Click(object sender, EventArgs e)

		/// <summary>
		/// Execute queries against selected instance and save result into project file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void executeButton_Click(object sender, EventArgs e)
		{
			AnalysisService asConn = new AnalysisService();
			try
			{
				Cursor.Current = Cursors.WaitCursor;
				if (asInstanceComboBox.Text == "Analysis Services 2000")
				{
					connStatus = asConn.TestConnection(projectDetails.AS2000, projectDetails.DatabaseName);
					if (connStatus)
					{
						if (as2kQueryStatus)
						{
							DialogResult result = MessageBox.Show("Do you want to clear the previously executed query result?", "AS Upgrade Verification Tool", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
							if (result == DialogResult.OK)
							{
								AnalysisService analysisService = new AnalysisService();
								analysisService.ExecuteQueries("AS2K", projectDetails);
								ListQuery();
								CustomizeGrid();
							}
						}
						else
						{
							AnalysisService analysisService = new AnalysisService();
							analysisService.ExecuteQueries("AS2K", projectDetails);
							as2kQueryStatus = true;
							ListQuery();
							CustomizeGrid();
						}
					}
					else
					{
						throw new CustomAppException("customException17");
					}
				}
				else
				{
					connStatus = asConn.TestConnection(projectDetails.AS2005, projectDetails.DatabaseName);
					if (connStatus)
					{
						if (as2k5QueryStatus)
						{
							DialogResult result = MessageBox.Show("Do you want to clear the previously executed query result?", "AS Upgrade Verification Tool", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
							if (result == DialogResult.OK)
							{
								AnalysisService analysisService = new AnalysisService();
								analysisService.ExecuteQueries("AS2K5", projectDetails);
								ListQuery();
								CustomizeGrid();
							}
						}
						else
						{
							AnalysisService analysisService = new AnalysisService();
							analysisService.ExecuteQueries("AS2K5", projectDetails);
							as2k5QueryStatus = true;
							ListQuery();
							CustomizeGrid();
						}
					}
					else
					{
						throw new CustomAppException("customException17");
					}
				}
			}
			catch (CustomAppException ex)
			{
				ExceptionManager.Publish(ex);
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error);
			}
			Cursor.Current = Cursors.Default;
		}
		#endregion

		#region private void removeAllButton_Click(object sender, EventArgs e)

		/// <summary>
		/// Remove all queries and its corresponding results from the project file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void removeAllButton_Click(object sender, EventArgs e)
		{
			DialogResult result = MessageBox.Show("Do you want to remove all the queries from this project?", "AS Upgrade Verification Tool", MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
			if (result == DialogResult.OK)
			{
				XmlDocument xmldomProjectFile = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
				xmldomProjectFile.SelectSingleNode("ProjectFile/MDXQueries").InnerText = "";
				xmldomProjectFile.SelectSingleNode("ProjectFile/TotalQueries").InnerText = "0";
				xmldomProjectFile.SelectSingleNode("ProjectFile/QueriesPassed").InnerText = "0";
				xmldomProjectFile.SelectSingleNode("ProjectFile/QueriesFailed").InnerText = "0";
				ProjectFile.SetProjectDetails(xmldomProjectFile, projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
				queryListDataGridView.DataSource = null;
				queryExist = false;
				customize = false;
				ListQuery();
				if (QueriesRemoved != null)
				{
					QueriesRemoved();
				}

			}
		}
		#endregion

		#region private void ASQueryListForm_Shown(object sender, EventArgs e)

		private void ASQueryListForm_Shown(object sender, EventArgs e)
		{

			CustomizeGrid();
		}
		#endregion

		#region private void CustomizeGrid()
		/// <summary>
		/// Customize DatagridView
		/// </summary>
		private void CustomizeGrid()
		{
			if (customize)
			{
				queryListDataGridView.Columns[0].Width = 75;
				queryListDataGridView.Columns[0].HeaderText = "Query Name";
				queryListDataGridView.Columns[1].Width = 100;
				queryListDataGridView.Columns[1].HeaderText = "Database Name";
				queryListDataGridView.Columns[2].Width = 290;
				queryListDataGridView.Columns[2].HeaderText = "MDX Query";
				queryListDataGridView.Columns[3].Width = 110;
				queryListDataGridView.Columns[3].HeaderText = "Query Status";
				queryListDataGridView.Columns[4].HeaderText = "Execution Time(millisec)";
				queryListDataGridView.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
				queryListDataGridView.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			}
			for (int counter = 0; counter <queryListDataGridView.Rows.Count; counter++)
			{
				queryListDataGridView.Rows[counter].ContextMenuStrip = removeContextMenuStrip;
			}
		}
		#endregion

		#region private void closeQueryListButton_Click(object sender, EventArgs e)

		private void closeQueryListButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion

		#region private void addQueryButton_Click(object sender, EventArgs e)
		/// <summary>
		/// load form to add queries to project file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void addQueryButton_Click(object sender, EventArgs e)
		{
			mainForm.ShowAddQuery();
		}
		#endregion

		#region private void removeToolStripMenuItem_Click(object sender, EventArgs e)

		private void removeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			try
			{
				if (queryListDataGridView.SelectedCells.Count != 0)
				{
					string queryName = queryListDataGridView.SelectedCells[0].Value.ToString();
					DialogResult result = MessageBox.Show("Do you want to remove the selected Query? - " + queryName, "AS Upgrade Verification Tool", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
					if (result == DialogResult.OK)
					{
						if (queryListDataGridView.Rows.Count == 1)
						{
							queryListDataGridView.Rows.Remove(queryListDataGridView.Rows[0]);
							queryExist = false;
							customize = false;
							if (QueriesRemoved != null)
							{
								QueriesRemoved();
							}
						}
						XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
						XmlNode xmlNodeToRemove = xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[Name=\"" + queryName.Trim() + "\"]");
						xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[Name=\"" + queryName.Trim() + "\"]").ParentNode.RemoveChild(xmlNodeToRemove);
						int totalqueries = int.Parse(xmlDomProjectContent.SelectSingleNode("ProjectFile/TotalQueries").InnerText, CultureInfo.CurrentCulture);
						totalqueries = totalqueries - 1;
						xmlDomProjectContent.SelectSingleNode("ProjectFile/TotalQueries").InnerText = totalqueries.ToString(CultureInfo.CurrentCulture);
						ProjectFile.SetProjectDetails(xmlDomProjectContent, projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
						ListQuery();
						CustomizeGrid();
					}
				}
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}

		}
		#endregion
	}
}
